/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2015-9-17 9:10:14
 ******************************************************************************/
package cn.cmcc.online.smsapi.sdk.net;

import java.net.HttpURLConnection;

import cn.cmcc.online.smsapi.sdk.Iservice.OnlineUpdateCycleConfigInterface;
import cn.cmcc.online.smsapi.sdk.Iservice.XyCallBack;
import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.db.entity.SysParamEntityManager;
import cn.cmcc.online.smsapi.sdk.dex.DexUtil;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
import cn.cmcc.online.smsapi.sdk.net.util.Signaturer;
import cn.cmcc.online.smsapi.sdk.util.RecordFunctionUtil;
import cn.cmcc.online.smsapi.sdk.util.StringUtils;
import cn.cmcc.online.smsapi.util.UUIDUtils;

/**
 * java.net.* 处理联网相关的
 * 
 * @author Administrator
 * 
 */
public class XyHttpRunnable extends BaseHttpRunnable {
    final static String TAG = "HTTP";
    String cnum;
    public static String appKeySign = null;
    public static String appKey = null;

    public XyHttpRunnable(String url, String content, String cnum, String cmd,
            boolean isLogin, XyCallBack callBack, Boolean isCompress) {

        super(url, null, content, isLogin, cmd, callBack, isCompress);
        this.cnum = cnum;
    }

    @Override
    public void setHttpHeader(HttpHeaderDto hhd, boolean isLogin, String cmd,
            HttpURLConnection http) throws Exception {
        http.addRequestProperty("Content-Type", "text/xml;UTF-8");
        if (!StringUtils.isNull(cnum)) {
            http.addRequestProperty("cnum", cnum);
        }
        String keySign = appKey;
        if (isLogin) {

            http.addRequestProperty("command", "2");
        } else {
            String token = SysParamEntityManager.getStringParam(
                    Constant.getContext(), Constant.HTTPTOKEN); // Constant.token;

            http.addRequestProperty("command", "1");
            if (!StringUtils.isNull(token)) {
                keySign = keySign + token;
                http.addRequestProperty("token", token);
            }
        }
        String app_key_sign = Signaturer.sign(appKeySign, keySign);
        http.addRequestProperty("app-key", appKey);
        http.addRequestProperty("app-key-sign", app_key_sign);
        http.addRequestProperty("compress", "1");
        http.addRequestProperty("loginid", "");
        http.addRequestProperty("recordState", RecordFunctionUtil.getrecordState());
        http.addRequestProperty("sdkversion", NetUtil.APPVERSION);// 客户端应用版本号
        
        /* SDK-452 zhaojiangwei 2016-05-16 start */
        http.addRequestProperty("abi", getDeviceABIS());
        http.addRequestProperty("uiversion", DexUtil.getUIVersion());
        /* SDK-452 zhaojiangwei 2016-05-16 end */
        
        /* SDK-602 zhaojiangwei 2016-09-08 start */
        String androidId = UUIDUtils.getAndroidId();
        String psuedoId = UUIDUtils.getUniquePsuedoID();
        String referer = DexUtil.getOnLineConfigureData(OnlineUpdateCycleConfigInterface.CONFIG_URL_REFERER);
        if (StringUtils.isNull(referer)) {
            referer = "bizport.cn/66dc91e8b78b1c284027a3eb1be0a70e";
        }
        http.addRequestProperty("ai", Signaturer.sha256Encode(androidId));
        http.addRequestProperty("ni", Signaturer.sha256Encode(psuedoId));
        http.addRequestProperty("referer",  referer);
        /* SDK-602 zhaojiangwei 2016-09-08 end */
        /*DATA-101 zhaojiangwei 20170309 start */
        http.addRequestProperty("net", String.valueOf(getCurrentNetType()));
        /*DATA-101 zhaojiangwei 20170309 end */
        if (LogManager.debug) {
            LogManager.e("httpheader", http.getRequestProperties().toString());
        }
        
        if (!StringUtils.isNull(cmd)) {
            http.addRequestProperty("cmd", cmd);
        }
        addHeadSign(http);
    }
}
